home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / drdobbs / ddjcompr / comp2_as / comp2.asm next >
Assembly Source File  |  1991-04-29  |  4KB  |  202 lines

  1.  
  2. ; Copyright 1991 by Jussi Puttonen, Timo Raita and Jukka Teuhola
  3.  
  4. ; Written by Jussi Puttonen, 19.4.1991 at University of Turku, Finland
  5. ; Algorithms suggested by Timo Raita and Jukka Teuhola
  6.  
  7.  
  8. InFile    equ    0
  9. OutFile    equ    1
  10.  
  11. ReadBufferSize        equ    16384
  12. WriteBufferSize    equ    16384
  13.  
  14. stack segment stack 'stack'
  15.         db 1024 dup (?)
  16. stack ends
  17.  
  18. data segment para public 'data'
  19.  
  20. FailString    db    'COMP failed.', 10,13, '$'
  21.  
  22.         align
  23.  
  24. ReadBuffer    db    ReadBufferSize dup (?)
  25. ReadBufferEnd    label byte
  26. ReadSize        dw    ?
  27.         align
  28.  
  29. WriteBuffer    db    WriteBufferSize dup (?)
  30. WriteBufferEnd    label byte
  31. ExtraWriteBuffer db 10 dup (?)
  32.  
  33. data ends
  34.  
  35. table segment para public 'data'
  36.     db    8000h dup (' ')
  37.     db    8000h dup (' ')
  38. table ends
  39.  
  40. code segment para public 'code'
  41.     assume cs:code, ds:data, es:table
  42.  
  43. ; FlushBuffer must be called at the end of the program
  44.  
  45. WriteBlock:    push    ax
  46.             push    bx
  47.             push    cx
  48.             push    dx
  49.             mov    ah, 40h
  50.             mov    bx, OutFile
  51.             mov    cx, WriteBufferSize
  52.             lea    dx, WriteBuffer
  53.             int    21h
  54.  
  55. ; take care of the ExtraWriteBuffer
  56.             mov    cx, di
  57.             mov    di, offset WriteBuffer
  58.             sub    cx, offset WriteBufferEnd
  59.             je    ExtraHandled
  60.             mov    bx, offset WriteBufferEnd
  61. ExtraCopyLoop:    mov    al, ds:[bx]
  62.             mov    ds:[di], al
  63.             inc    bx
  64.             inc    di
  65.             loop    ExtraCopyLoop
  66. ExtraHandled:    pop    dx
  67.             pop    cx
  68.             pop    bx
  69.             pop    ax
  70.             ret
  71.  
  72. FlushBuffer    proc
  73.             mov    ah, 40h
  74.             mov    bx, OutFile
  75.             mov    cx, di
  76.             lea    dx, WriteBuffer
  77.             sub    cx, dx
  78.             int    21h
  79.             ret
  80. FlushBuffer    endp
  81.  
  82. ; register use:
  83. ;   bx    address
  84. ;   si    pointer to ReadBuffer
  85. ;   di    pointer to WriteBuffer
  86. ;   dl    Bits
  87. ;   bp    SavedWriteLoc
  88.  
  89. jProcessLastBytes:
  90.             jmp    ProcessLastBytes
  91. Start:        cld
  92.             mov    ax, data
  93.             mov    ds, ax
  94.             mov    ax, table
  95.             mov    es, ax
  96.  
  97.             mov    bx, 0
  98.             mov    di, offset WriteBuffer
  99.  
  100. ReadBlockLoop:    mov    dx, offset ReadBuffer
  101.             mov    si, dx                ; to be used in ProcessBlockLoop
  102.             push    bx
  103.             mov    bx, InFile
  104.             mov    ah, 3Fh
  105.             mov    cx, ReadBufferSize
  106.             int    21h
  107.             jnc    ReadSuccess
  108.             jmp    fail
  109. ReadSuccess:    pop    bx
  110.             mov    ReadSize, ax
  111.             mov    cl, 3
  112.             shr    ax, cl
  113.             je    jProcessLastBytes
  114.             mov    cx, ax
  115.  
  116. ProcessByte    macro SourceReg,BitVal
  117.             local over
  118.             cmp    SourceReg, es:[bx]
  119.             je    over
  120.             or    dl, BitVal
  121.             mov    es:[bx], SourceReg
  122.             mov    ds:[di], SourceReg
  123.             inc    di
  124. over:        mov    bh, bl
  125.             mov    bl, SourceReg
  126.             endm
  127.  
  128.  
  129. ProcessBlockLoop:
  130.             mov bp, di
  131.             inc di
  132.             xor dl, dl
  133.             lodsw
  134.             ProcessByte al, 80h
  135.             ProcessByte ah, 40h
  136.             lodsw
  137.             ProcessByte al, 20h
  138.             ProcessByte ah, 10h
  139.             lodsw
  140.             ProcessByte al, 08h
  141.             ProcessByte ah, 04h
  142.             lodsw
  143.             ProcessByte al, 02h
  144.             ProcessByte ah, 01h
  145.  
  146.             mov    ds:[bp], dl
  147.  
  148.             cmp    di, offset WriteBufferEnd
  149.             jb    NoWriteBlock
  150.             call    WriteBlock
  151. NoWriteBlock:    loop    jProcessBlockLoop
  152.             jmp    jNext1
  153. jProcessBlockLoop:
  154.             jmp    ProcessBlockLoop
  155. jNext1:        mov    ax, ReadSize
  156.             cmp    ax, ReadBufferSize
  157.             jne    ProcessLastBytes
  158.             jmp    ReadBlockLoop
  159. ProcessLastBytes:
  160.             mov    cx, ReadSize
  161.             and    cx, 7
  162.             je    Finish
  163.             mov    bp, di
  164.             inc    di
  165. LastByteLoop:    lodsb
  166.             shl    dl, 1
  167.             cmp    al, es:[bx]
  168.             je    over
  169.             inc    dl
  170.             mov    es:[bx], al
  171.             mov    ds:[di], al
  172.             inc    di
  173. over:        mov    bh, bl
  174.             mov    bl, al
  175.             loop    LastByteLoop
  176.  
  177.             mov    cx, 8
  178.             sub    cx, ReadSize
  179.             and    cx, 7
  180.             je    NoShift
  181. ShiftLoop:    shl    dl, 1
  182.             inc    dl
  183.             loop    ShiftLoop
  184.  
  185. NoShift:        mov    ds:[bp], dl
  186.  
  187. Finish:        call    FlushBuffer
  188.  
  189.             mov    ax, 4c00h
  190.             int    21h
  191.  
  192. fail:        mov    dx, offset FailString
  193.             mov    ah, 9
  194.             int    21h
  195.             mov    ax, 4c01h
  196.             int    21h
  197.  
  198.  
  199.             code ends
  200.  
  201.         end Start
  202.